{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "CxmDMK4yupqg"
   },
   "source": [
    "# 物体识别\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "v4XGxDrCkeip"
   },
   "source": [
    "## 1.初始化\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "cellView": "both",
    "colab": {},
    "colab_type": "code",
    "id": "6cPY9Ou4sWs_"
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"..\")\n",
    "import tensorflow as tf\n",
    "import common\n",
    "import time\n",
    "\n",
    "print(\"当前Tensorflow版本: %s\" % tf.__version__)\n",
    "print(\"当前可用GPU设备: %s\" % tf.test.gpu_device_name())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "t-VdfLbC1w51"
   },
   "source": [
    "## 2.加载已训练的模块\n",
    "* **openimages_v4**: 精度高，速度慢\n",
    "* **mobilenet_v2**: 精度低，速度快\n",
    "* 可自行切换，替换load内的字符即可\n",
    "* 加载速度较慢，需要等待  In[ ]  中的*号变成具体的数字才算完成（速度较慢，请耐心等待[大约一分钟])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "uazJ5ASc2_QE"
   },
   "outputs": [],
   "source": [
    "#mobilenet_v2/openimages_v4\n",
    "detector = tf.saved_model.load(\"openimages_v4\").signatures['default']\n",
    "\n",
    "def detect_img(image_url):\n",
    "    start_time = time.time()\n",
    "    image_path = common.download_and_resize_image(image_url, 640, 480)\n",
    "    common.run_detector(detector, image_path)\n",
    "    end_time = time.time()\n",
    "    print(\"预测时长:\",round(end_time-start_time, 3),\"秒\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "WUUY3nfRX7VF"
   },
   "source": [
    "## 3.预测物体\n",
    "* detect_img中的url可以自行替换\n",
    "* 没有使用GPU时，速度较慢，请耐心等待（大约30秒）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "otPnrxMKIrj5"
   },
   "outputs": [],
   "source": [
    "detect_img(\"https://upload.wikimedia.org/wikipedia/commons/1/1b/The_Coleoptera_of_the_British_islands_%28Plate_125%29_%288592917784%29.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "H5F7DkD5NtOx"
   },
   "outputs": [],
   "source": [
    "detect_img(\"https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Biblioteca_Maim%C3%B3nides%2C_Campus_Universitario_de_Rabanales_007.jpg/1024px-Biblioteca_Maim%C3%B3nides%2C_Campus_Universitario_de_Rabanales_007.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "DZ18R7dWNyoU"
   },
   "outputs": [],
   "source": [
    "detect_img(\"https://upload.wikimedia.org/wikipedia/commons/0/09/The_smaller_British_birds_%288053836633%29.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "Object detection",
   "private_outputs": true,
   "provenance": [],
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}